package com.example.onlinemusic.config;

import com.example.onlinemusic.constant.Constant;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        HttpSession httpSession = request.getSession(false);
        if (httpSession != null &&
                httpSession.getAttribute(Constant.LOGINUSER_SESSION_KEY) != null) {
//此时是登录状态
            System.out.println("曹操次奥");
            return true;
        }
        response.setStatus(401);
        response.sendRedirect("/login.html");
        return false;
    }
}
